using BusinessLogic.Managers.Processing.ProcessingExceptions;
using BusinessLogic.Managers.Processing.RemittanceAdvice.Exceptions;
using BusinessLogic.Mappers;
using DataAccess.Services;

namespace BusinessLogic.Managers.Processing.RemittanceAdvice.ValidationRules
{
	internal class RemittanceAdvicePricingDetailsValidationRule : IRemittanceAdviceValidationRule
	{
		public void Validate(BusinessEntity.Entities.RemittanceAdvice remittanceAdvice, IServiceManager serviceManager)
		{
			ValidateDescriptionExistance(remittanceAdvice.Description);
			ValidateCurrencyExistence(remittanceAdvice, serviceManager);
			ValidateTotal(remittanceAdvice.TotalAmount);
		}

		private static void ValidateDescriptionExistance(string description)
		{
			if(string.IsNullOrEmpty(description))
			{
				throw new RemittanceAdviceEmptyDescriptionException();
			}
		}

		private static void ValidateCurrencyExistence(BusinessEntity.Entities.RemittanceAdvice remittanceAdvice, IServiceManager serviceManager)
		{
			if (!MapperFactory.GetCurrencyMapper(serviceManager).IsSupported(remittanceAdvice.Currency.Code))
			{
				throw new CurrencyDoesntExistException(remittanceAdvice.Currency);
			}

			remittanceAdvice.Currency = MapperFactory.GetCurrencyMapper(serviceManager).GetByCode(remittanceAdvice.Currency.Code);
		}

		private static void ValidateTotal(decimal amount)
		{
			if(amount <= decimal.Zero)
			{
				throw new InvalidRemittanceAdviceTotalAmountException();
			}
		}
	}
}